<!--  -->
<template>
  <div class="login">
    <div class="header">
      <van-icon name="arrow-left" @click="gologin" />
      <span>账号登录</span>
      <span></span>
    </div>
    <div class="main">
      <van-form @submit="onSubmit">
        <van-field
          v-model="useInfo.username"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="useInfo.password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <div style="margin: 16px;">
          <van-button class="butn" block type="info" native-type="submit"
            >提交</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      useInfo: {
        username: "",
        password: "",
      },
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    gologin() {
      this.router.go(-1);
    },
    onSubmit(values) {
      console.log(values);
      axios({
        url: "http://47.102.145.189:8009/user/login",
        method: "post",
        data: this.useInfo,
      }).then((res) => {
        console.log(res);
        if (res.data.status === 400) {
          alert("你的账号或者密码异常");
        } else {
          const token = res.data.body;
          console.log(token);
          window.localStorage.setItem("token", "yuscs");
          this.$router.push("my");
        }
      });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
};
</script>
<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  .header {
    width: 100%;
    height: 50px;
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    align-items: center;
    font-size: 18px;
    .van-icon {
      font-size: 20px;
    }
  }
  .main {
    width: 100%;
    flex: 1;
    .butn {
      background: #1cb676;
      color: #fff;
      font-size: 18px;
    }
  }
}
</style>
